<?php
/**
 * @Author: Wang Chunsheng 2192138785@qq.com
 * @Date:   2020-03-15 16:08:26
 * @Last Modified by:   Wang Chunsheng 2192138785@qq.com
 * @Last Modified time: 2020-03-16 01:25:47
 */
 

namespace app\modules\diandi_shop\models\searchs;

use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\diandi_shop\models\DdOrder;
use common\helpers\DateHelper;
use common\helpers\ErrorsHelper;

/**
 * DdOrderSearch represents the model behind the search form of `app\modules\diandi_shop\models\DdOrder`.
 */
class DdOrderSearch extends DdOrder
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['order_id', 'pay_status', 'pay_time', 'delivery_status', 'delivery_time', 'receipt_status', 'receipt_time', 'order_status', 'user_id', 'wxapp_id', 'create_time', 'update_time'], 'integer'],
            [['order_no', 'express_company', 'express_no', 'transaction_id'], 'safe'],
            [['total_price', 'pay_price', 'express_price'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {

        $query = DdOrder::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        if(!empty($params['DdOrderSearch']['create_time'])){
            $this->create_time = $params['DdOrderSearch']['create_time'] = strtotime($params['DdOrderSearch']['create_time']);
        }

        $this->load($params);
        if (!$this->validate()) {
            return $dataProvider;
        }
        // grid filtering conditions
        $query->andFilterWhere([
            'order_id' => $this->order_id,
            'total_price' => $this->total_price,
            'pay_price' => $this->pay_price,
            'pay_status' => $this->pay_status,
            'pay_time' => $this->pay_time,
            'express_price' => $this->express_price,
            'delivery_status' => $this->delivery_status,
            'delivery_time' => $this->delivery_time,
            'receipt_status' => $this->receipt_status,
            'receipt_time' => $this->receipt_time,
            'order_status' => $this->order_status,
            'user_id' => $this->user_id,
            'wxapp_id' => $this->wxapp_id,
            // 'create_time' => $this->create_time,
            'update_time' => $this->update_time,
        ]);
        

        $ago = DateHelper::dayAgo($this->create_time);
        $query->andFilterWhere(['like', 'order_no', $this->order_no])
            ->andFilterWhere(['like', 'express_company', $this->express_company])
            ->andFilterWhere(['like', 'express_no', $this->express_no])
            ->andFilterWhere(['like', 'transaction_id', $this->transaction_id])
            // ->andFilterWhere(['like', 'create_time', $this->create_time])
            ->andFilterWhere(['between', 'create_time',$ago['start'],$ago['end']])
            ->orderBy('create_time desc');
        
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);
        return $dataProvider;
    }
}
